package com.example.limit.mapper;

import com.example.limit.entity.PurchaseRule;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PurchaseRuleMapper {
    
    @Insert("INSERT INTO purchase_rule(rule_name, rule_type, sub_type, limit_quantity, target_value, start_time, end_time, status, create_time, update_time) " +
            "VALUES(#{ruleName}, #{ruleType}, #{subType}, #{limitQuantity}, #{targetValue}, #{startTime}, #{endTime}, #{status}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(PurchaseRule rule);
    
    @Update("UPDATE purchase_rule SET rule_name=#{ruleName}, rule_type=#{ruleType}, sub_type=#{subType}, limit_quantity=#{limitQuantity}, " +
            "target_value=#{targetValue}, start_time=#{startTime}, end_time=#{endTime}, status=#{status}, update_time=NOW() WHERE id=#{id}")
    int update(PurchaseRule rule);
    
    @Delete("DELETE FROM purchase_rule WHERE id=#{id}")
    int delete(Long id);
    
    @Select("SELECT * FROM purchase_rule WHERE id=#{id}")
    PurchaseRule selectById(Long id);
    
    @Select("SELECT * FROM purchase_rule WHERE status=1 AND start_time <= NOW() AND end_time >= NOW()")
    List<PurchaseRule> selectActiveRules();
    
    @Select("SELECT * FROM purchase_rule WHERE rule_type=#{ruleType} AND status=1 AND start_time <= NOW() AND end_time >= NOW()")
    List<PurchaseRule> selectByRuleType(Integer ruleType);
}